package dream.zyp.ballgame.activity;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import dream.zyp.ballgame.component.ViewInfo;
import dream.zyp.ballgame.tools.TouchAction;

/**
 * Created by ZYP on 2017/3/4.
 */
public class BallView extends SurfaceView implements SurfaceHolder.Callback{
    float nowPosition = 0;
    private ViewInfo viewInfo;

    private DrawThread thread;
    private Handler transmitHandler;

    public BallView(Context context) {
        super(context);
    }

    public BallView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.i("MEASURE", "onMeasure: "+widthMeasureSpec+heightMeasureSpec);
    }


    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Log.e("layout", "onLayout: "+left+" "+top+" "+right+" "+bottom );
        viewInfo = new ViewInfo();
        viewInfo.setWidth((right - left));
        viewInfo.setHeight((bottom - top));
        //callBack = new ViewCallBack(info);
       // transmitHandler = callBack.getTransmitHandler();
        this.getHolder().addCallback(this);

    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (nowPosition == 0){
            nowPosition = getWidth() / 2;
        }

        if (transmitHandler != null){
            switch (event.getAction()){
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_DOWN:
                    if (event.getX() < nowPosition)
                        transmitHandler.sendEmptyMessageDelayed(TouchAction.LEFT, 10);
                    else
                        transmitHandler.sendEmptyMessage(TouchAction.RIGHT);
                    nowPosition = event.getX(); //更新位置坐标
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (event.getX() < nowPosition)
                        transmitHandler.sendEmptyMessage(TouchAction.LEFT);
                    else
                        transmitHandler.sendEmptyMessage(TouchAction.RIGHT);
                    nowPosition = event.getX(); //更新位置坐标

            }
                // 如果是按下状态


        }
        return true;
    }

    public Handler getTransmitHandler(){
        return transmitHandler;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        thread = new DrawThread(holder);
        thread.setViewInfo(viewInfo);
        transmitHandler = thread.getControlHandler();
        thread.start();
    }


    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }


    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
